package controller.site;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.Student;
import dao.Teacher;

/**
 * Servlet implementation class login
 */
@WebServlet("/login_t")
public class login_t extends Base {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public login_t() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        view.display("login_t.jsp");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String no = request.getParameter("no");
        String pwd = request.getParameter("pwd");
        try {
            Teacher.Data d = Teacher.findByNoAndPwd(no, pwd);
            if (d == null) {
                view.displayMap("login.jsp", "msg", "密码错误");
            } else {
                HttpSession session = request.getSession();
                session.setAttribute("user-type", "teacher");
                session.setAttribute("user-data", d);
                view.sendRedirect("teacher/manager/index");
            }
        } catch (SQLException e) {
            view.display("error/sqlerror.jsp", e.getMessage());
            e.printStackTrace();
        }
    }
}
